#include "PageWaterMode.h"
#include "R.h"
#include "wind_mgr.h"
#include "tools.h"
#include "conf_mgr.h"
#include "zzCommon.h"

PageWaterMode::PageWaterMode(ViewGroup *wind_page_box)
    : PageBase(wind_page_box, "@layout/page_recyclerview_test", PAGE_WATER_MODE)
{

    // ImageView *backToHome = (ImageView *)wind_page_box->findViewById(demo::R::id::back);
    // backToHome->setOnClickListener([this](View &){
    //     d_windMgr->showPage(PAGE_MODE1);
    // });
    
    // ImageView *backToHome2 = (ImageView *)wind_page_box->findViewById(demo::R::id::home);
    // backToHome2->setOnClickListener([this](View &){
    //     d_windMgr->showPage(PAGE_MODE1);
    // });

    // ViewGroup *waterModeAdd = (ViewGroup *)wind_page_box->findViewById(demo::R::id::mode_add);

    mTools->setOnClickListenerToShowPage((ViewGroup *)this->dPageLayout->findViewById(demo::R::id::back), PAGE_MODE1);
    mTools->setOnClickListenerToShowPage((ViewGroup *)this->dPageLayout->findViewById(demo::R::id::home), PAGE_MODE1);
    mTools->setOnClickListenerToShowPage((ViewGroup *)this->dPageLayout->findViewById(demo::R::id::mode_add), PAGE_WATER_MODE_ADD);
    // this->dPageLayout->findViewById(demo::R::id::mode_add)->setOnClickListener([this](View &){
    //     d_windMgr->showPage(PAGE_WATER_MODE_ADD, true, 1, -1);
    // });

    mLine = (ViewGroup *)this->dPageLayout->findViewById(demo::R::id::main_line);
    recyclerView = (RecyclerView *)this->dPageLayout->findViewById(demo::R::id::recyclerview_layout21);
    linnearMg = new LinearLayoutManager(recyclerView->getContext(), LinearLayout::HORIZONTAL, false);
    recyclerView->setLayoutManager(linnearMg);
    recyclerView->getLayoutManager()->setItemPrefetchEnabled(true);
    
    mRvAdapter = new RecyclerViewAdapter();
    recyclerView->setAdapter(mRvAdapter);
    recyclerView->getRecycledViewPool().setMaxRecycledViews(0, 10);
    // recyclerView->setOverScrollMode(View::OVER_SCROLL_ALWAYS);

    // DividerItemDecoration *decoration = new DividerItemDecoration(recyclerView->getContext(), LinearLayout::HORIZONTAL);
    // recyclerView->addItemDecoration(decoration);

    recyclerView->setItemAnimator(nullptr);
    // RecyclerView *recyclerView = (ViewGroup *)this->dPageLayout->findViewById(demo::R::id::recyclerview_layoutxxx);

    // recyclerView->setHorizontalScrollBarEnabled(true);

    
    setRecyclerViewScrollLine(recyclerView, mLine, LinearLayout::HORIZONTAL);
    // setRecyclerViewScrollLine(recyclerView);
}

// void PageWaterMode::setRecyclerViewScrollLine(RecyclerView* mRvHx){
//     RecyclerView::OnScrollListener scrollListener;
//     scrollListener.onScrolled = [this](RecyclerView& recyclerView, int dx, int dy){       
//         LOGV("onScrolled  dx = %d, dy = %d", dx, dy);
//         updateScrollBar(&recyclerView, mLine);
//         // mLine->getLayoutParams()->width = (recyclerView.getWidth() / recyclerView.computeHorizontalScrollRange()) * mLine->getLayoutParams()->width;
//         // int range = recyclerView.computeHorizontalScrollRange();
//         // float maxEndX = range - recyclerView.getWidth();
//         // sx += dx;
//         // endX += dx;
//         // float proportion = endX / maxEndX;
//         // LOGV("onScrolled222  dx = %d, dy = %d endX = %f, range = %d", dx, dy, endX, range);
//         // int transMaxRange = ((ViewGroup *)mLine->getParent())->getWidth() - mLine->getWidth();
//         // mLine->setTranslationX(transMaxRange * proportion);
//     };
//     // scrollListener.onScrollStateChanged = [this](RecyclerView& recyclerView, int newState){
//     //     LOGV("newState = %d", newState);
//     //     if (newState == RecyclerView::SCROLL_STATE_IDLE)
//     //     {
//     //         updateScrollBar(recyclerView);
//     //         // x = recyclerView.computeHorizontalScrollOffset();
//     //         // y = recyclerView.computeVerticalScrollOffset();
//     //         // LOGV("onScrollStateChanged  dx = %d, dy = %d", x, y);
//     //         // endX = endX + x - sx;
//     //     }
        
//     // };
//     recyclerView->addOnScrollListener(scrollListener);
// }

// void PageWaterMode::updateScrollBar(RecyclerView* recyclerView)
// {
//     int offset = recyclerView->computeHorizontalScrollOffset();
//     int range = recyclerView->computeHorizontalScrollRange();
//     int extent = recyclerView->computeHorizontalScrollExtent();

//     float percentage = (float) offset / (float) (range - extent);
//     int parentWidth = (mLine->getParent())->getWidth();
//     int mLineWidth = mLine->getWidth();
//     int position = (int) (percentage * (parentWidth - mLineWidth));
//     mLine->setTranslationX(position);
// }

PageWaterMode::~PageWaterMode()
{
    delete recyclerView;
    delete linnearMg;
    delete mRvAdapter;
}

void PageWaterMode::startPageAction(pageType type, int position){
    mRvAdapter->setModeDataList(d_fileLoad->getModeDataList());
    mRvAdapter->notifyDataSetChanged();
}